function ecPage(_id, _url, _p, _param) {
	var $id = _id;
	var $url = _url;
	var $p = new Pagination(_p);
	var $param = _param;
	var $anchor = "" || _param["anchor"];
	var $href = "";
	var syn  = "true";

	var $random = new Date().getTime();

	function init() {
		if($p.itemsCount <= $p.pageSize) {
			return false;
		}
		syn = _param.syn;
		if(syn === "false") {
			 createHtml();
			 bindAjaxClick();
		} else {
			createHref();
			createHtml();
			bindClick();
		}
	}

	function createHtml() {
		if (!exist($param.pageStyle)) {
			createHtml_default();
		} else {
			try {
				eval("createHtml_" + $param.pageStyle + "()");
			}catch (e){
				createHtml_default();
			}
		}
	}
	
	function bindClick() {
		$(".page-ec a").on("click", function() {
			var obj = $(this);
			if(obj.hasClass("a-prev-disable") || obj.hasClass("pm-current") || obj.hasClass("a-next-disable")) {
				return false;
			}
		});
		//点击确定
		$(".page-ec .pm-sure").on("click", function() {
			var skipPage = $(this).prev().children("input").val();
			if (skipPage > $p.pageCount) {
				skipPage = $p.pageCount;
			}
			$href = $href + "&p.pageNo=" + skipPage;
			window.location.href = $href + "&r=" + Math.random() + "#" + $anchor;
		});
	}
	
	function createHref() {
		var formData = createParam();
		var str = $url.indexOf("?")== -1 ? "?" : "&";
		if ("true" == $param.encodeURI){
			var f = new Array();
			var s = formData.split("&");
			for (var i = 0; i < s.length; i++){
				var k = s[i].split("=");
				f.push(k[0]);
				f.push("=");
				f.push(encodeParam(k[1]));
				if (i < s.length - 1){
					f.push("&");
				}
			}
			formData = f.join("");
		}
		$href = $url + str + formData 
		+ "&p.pageSize=" + $p.pageSize
		+ "&p.itemsCount=" + $p.itemsCount;
		//+ "&p.pageNo=" + $(this).attr("value")
		//+ "#" + $anchor;
	}
	
	function bindAjaxClick() {
		var currPageNo = $p.pageNo;
		$("#"+$id).on("click", ".page-ec a", function() {
			var obj = $(this);
			if(obj.hasClass("a-prev-disable") || obj.hasClass("pm-current") || obj.hasClass("a-next-disable")) {
				return false;
			}
			if(obj.hasClass("pm-prev")) {
				 $p.pageNo =  $p.pageNo - 1;
			} else if(obj.hasClass("pm-next")) {
				 $p.pageNo =  $p.pageNo + 1;
			} else {
				$p.pageNo = obj.text();
			}
			createHtml();
			loadAjaxData($p.pageNo);
		});
		//点击确定
		$("#"+$id).on("click", ".page-ec .pm-sure", function() {
			var skipPage = $(this).prev().children("input").val();
			if (skipPage > $p.pageCount) {
				skipPage = $p.pageCount;
			}
			$p.pageNo = skipPage;
			createHtml();
			loadAjaxData($p.pageNo);
		});
		
	}
	
	
	function loadAjaxData(currPageNo) {
		var jsonData = createAjaxParam(currPageNo);
		$.ajax({
			type : "post",
			url : $url,
			data : jsonData,
			error : function(jqXHR, textStatus, errorThrown) {
				alert("error:" + errorThrown);
			},
			success : function(data, textStatus, jqXHR) {
				if ($param.ajaxback != null && exist(data)) {
					var callback = eval($param.ajaxback);
					data = eval("(" + data + ")");
					callback(data);
				}
			}
		});
	}
	
	function createAjaxParam(currPageNo) {
		var jsonData = new Array();
		if (exist($param.formId)) {
			jsonData = $("#" + $param.formId).serializeArray();
		}
		if (exist($param.formData)) {
			if (typeof $param.formData != "object") {
				$param.formData = eval("(" + $param.formData + ")");
			}
			for ( var i in $param.formData) {
				jsonData.push({
					"name" : i,
					"value" : $param.formData[i]
				});
			}
		}
		if (exist($param.inputIds)) {
			var ids = $param.inputIds.split(",");
			for ( var i = 0, length = ids.length; i < length; i++) {
				jsonData.push({
					"name" : ids[i],
					"value" : $("#" + ids[i]).val()
				});
			}
		}
		jsonData.push({
			"name" : "p.pageNo",
			"value" : currPageNo
		});
		jsonData.push({
			"name" : "p.pageSize",
			"value" : $p.pageSize
		});
		jsonData.push({
			"name" : "p.itemsCount",
			"value" : $p.itemsCount
		});
		return jsonData;
	}
	
	function createParam() {
		var formData = "";
		if (exist($param.formId)) { 
			formData += "&"+ $("#" + $param.formId).serialize();
		}
		if (exist($param.formData)) {
			if (typeof $param.formData != "object") {
				$param.formData = eval("(" + $param.formData + ")");
			}
			for(var i in $param.formData) {
				formData += "&" + i + "=" + $param.formData[i];
			}
		}
		if (exist($param.inputIds)) {
			var ids = $param.inputIds.split(",");
			for (var j = 0,length = ids.length; j < length; j++) {
				formData += "&" + ids[j] + "=" + $("#" + ids[j]).val();
			}
		}
		return formData;
	}
	
	function encodeParam(uri){
		//因tomcat会自动解码1次，为保证正确性，编码2次
		var s = encodeURIComponent(uri);
		var r = encodeURIComponent(s);
		return s;
	}
	
	function exist(obj) {
		return (obj != undefined && obj != null && obj != "");
	}
	
	function createHtml_default() {
		var page = [];
		var pageItem = 5;
		page.push("<div class=\"page-ec\">");
		if ($p.pageNo == 1) {
			page.push("<a class=\"pm-prev a-prev-disable\"  href="+$href+"&p.pageNo="+$p.prev+"#"+$anchor+"><i></i>上一页</a>");
		} else {
			page.push("<a class=\"pm-prev\" href="+$href+"&p.pageNo="+$p.prev+"#"+$anchor+"><i></i>上一页</a>");
		}
		//总页数小等于3
		if ($p.pageCount <= 3) {
			for (var i = 1, iSize = $p.pageCount; i <= iSize; i++) {
				if (i == $p.pageNo) {
					page.push("<a class=\"pm-current\" href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
				} else {
					page.push("<a href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
				}
			}
		}
		//总页数大于3
		if ($p.pageCount > 3) {
			if((pageItem + 2) < $p.pageCount){
				if (($p.pageNo + 2) > (pageItem + 2)){
					if(($p.pageNo + 2) < $p.pageCount){
						page.push("<a href="+$href+"&p.pageNo="+1+"#"+$anchor+">1</a>");
						page.push("<a href="+$href+"&p.pageNo="+2+"#"+$anchor+">2</a>");
						page.push("<strong>...</strong>");
						for (var i = ($p.pageNo - 2), iSize = $p.pageNo + 2; i <= iSize; i++) {
							if (i == $p.pageNo) {
								page.push("<a class=\"pm-current\" href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
							} else {
								page.push("<a href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
							}
						}
						page.push("<strong>...</strong>");
					} else {
						page.push("<a href="+$href+"&p.pageNo="+1+"#"+$anchor+">1</a>");
						page.push("<a href="+$href+"&p.pageNo="+2+"#"+$anchor+">2</a>");
						page.push("<strong>...</strong>");
						for (var i = ($p.pageCount - 2), iSize = $p.pageCount; i <= iSize; i++) {
							if (i == $p.pageNo) {
								page.push("<a class=\"pm-current\" href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
							} else {
								page.push("<a href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
							}
						}
					}
				} else {
					for (var i = 1, iSize = $p.pageNo + 2; i <= iSize; i++) {
						if (i == $p.pageNo) {
							page.push("<a class=\"pm-current\" href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
						} else {
							page.push("<a href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
						}
					}
					page.push("<strong>...</strong>");
				}
				
			} else {
				if ($p.pageNo + 2 < $p.pageCount) {
					for (var i = 1, iSize = $p.pageNo + 2; i <= iSize; i++) {
						if (i == $p.pageNo) {
							page.push("<a class=\"pm-current\" href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
						} else {
							page.push("<a href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
						}
					}
					page.push("<strong>...</strong>");
				} else {
					for (var i = 1, iSize = $p.pageCount; i <= iSize; i++) {
						if (i == $p.pageNo) {
							page.push("<a class=\"pm-current\" href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
						} else {
							page.push("<a href="+$href+"&p.pageNo="+i+"#"+$anchor+">"+i+"</a>");
						}
					}
				}
			}
		}
		if ($p.pageNo == $p.pageCount) {
			page.push("<a class=\"pm-next a-next-disable\" href="+$href+"&p.pageNo="+$p.next+"#"+$anchor+">下一页<i></i></a>");
		} else {
			page.push("<a class=\"pm-next\" href="+$href+"&p.pageNo="+$p.next+"#"+$anchor+">下一页<i></i></a>");
		}
		page.push("<span>共"+$p.pageCount+"页</span>");
		page.push("<span><label>到第</label><input  type=\"text\" value="+$p.pageNo+" /><label>页</label></span>");
		page.push("<b class=\"pm-sure\">确定</b>");
		$("#"+$id).html(page.join(""));
		if(syn === "false") {
			$(".page-ec a").attr("href", "javascript:void(0)");
		}
	}
	
	/**
	 * 重新初始化分页
	 */
	function rePage(p) {
		$p = new Pagination(p);
		createHtml();
	}
	
	//调用初始化方法
	init();
	
	
	
	return {
		"rePage": rePage
	};
}

function Pagination(_p) {
	if (!exist(_p)) {
		_p = {};
	}
	this.pageSize = parseInt(_p.pageSize) || 10;
	this.pageNo = parseInt(_p.pageNo) || 1;
	this.itemsCount = parseInt(_p.itemsCount) || 0;
	this.pageCount = parseInt((this.itemsCount - 1) / this.pageSize) + 1;
	this.prev = this.pageNo - 1;
	this.next = this.pageNo + 1;
	this.init = function() {
		if (this.pageNo <= 1) {
			this.pageNo = 1;
			this.prev = 1;
		}

		if (this.pageNo >= this.pageCount) {
			this.pageNo = this.pageCount;
			this.next = this.pageCount;
		}
	}
	this.init();

	this.refresh = function(_pageSize, _pageNo, _itemsCount) {
		this.pageSize = parseInt(_pageSize) || 10;
		this.pageNo = parseInt(_pageNo) || 1;
		this.itemsCount = parseInt(_itemsCount) || 0;
		this.pageCount = parseInt((this.itemsCount - 1) / this.pageSize) + 1;
		this.prev = this.pageNo - 1;
		this.next = this.pageNo + 1;
		this.init();
	}

	function exist(obj) {
		return (obj != undefined && obj != null && obj != "");
	}
}